package io.github.handyang.algorithms.sort;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.slf4j.LoggerFactory;

import java.util.Arrays;
import java.util.Random;

@Slf4j
public class SortExample {

    static {
        LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
        Logger rootLogger = loggerContext.getLogger("root");
        rootLogger.setLevel(Level.INFO);
    }

    private int[] randomArray() {
        Random random = new Random(System.currentTimeMillis());

        int[] res = new int[10];
        for (int i = 0; i < res.length; i++) {
            res[i] = random.nextInt(1000);
        }

        return res;
    }

    private void testSort(SortAlgorithms algorithms) {
        int[] array = randomArray();
        int[] copy = Arrays.copyOf(array, array.length);

        algorithms.sort(array);
        Arrays.sort(copy);

        algorithms.show(array);
        algorithms.show(copy);
        assert Arrays.equals(array, copy);

    }

    @Test
    public void testSelection() {
        SortAlgorithms algorithms = new SelectionSortAlgorithms();
        testSort(algorithms);
    }

    @Test
    public void testInsert() {
        SortAlgorithms algorithms = new InsertSortAlgorithms();
        testSort(algorithms);
    }

    @Test
    public void testShell() {
        SortAlgorithms algorithms = new ShellSortAlgorithms();
        testSort(algorithms);
    }

    @Test
    public void testMerge() {
        SortAlgorithms algorithms = new MergeSortAlgorithms();
        testSort(algorithms);
    }

    @Test
    public void testQuick() {
        SortAlgorithms algorithms = new QuickSortAlgorithms();
        testSort(algorithms);
    }

    @Test
    public void testHeap() {
        SortAlgorithms algorithms = new HeapSortAlgorithms();
        testSort(algorithms);
    }

    @Test
    public void testThreeWayQuick() {
        SortAlgorithms algorithms = new ThreeWayQuickSortAlgorithms();
        testSort(algorithms);
    }

    @Test
    public void testIterationQuick() {
        SortAlgorithms algorithms = new IterationQuickSortAlgorithms();
        testSort(algorithms);
    }

    @Test
    public void testBubble() {
        SortAlgorithms algorithms = new BubbleSortAlgorithms();
        testSort(algorithms);
    }

    @Test
    public void testBucket() {
        SortAlgorithms algorithms = new BucketSortAlgorithms();
        testSort(algorithms);
    }

    @Test
    public void testLSDRadix() {
        SortAlgorithms algorithms = new LSDRadixSortAlgorithms();
        testSort(algorithms);
    }

    @Test
    public void testMSDRadix() {
        SortAlgorithms algorithms = new MSDRadixSortAlgorithms();
        testSort(algorithms);
    }

    @Test
    public void testCount() {
        SortAlgorithms algorithms = new CountSortAlgorithms();
        testSort(algorithms);
    }

}
